package com.dream.dynamic_datasource.config.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;


/**
 * @author emiyakiri
 */
@ConfigurationProperties("env")
public class EnvProperties {
    private DozerProperties dozer = new DozerProperties();

    public DozerProperties getDozer() {
        return dozer;
    }

    public void setDozer(DozerProperties dozer) {
        this.dozer = dozer;
    }

    public class DozerProperties {
        private String mappingLocation;

        public String getMappingLocation() {
            return mappingLocation;
        }

        public void setMappingLocation(String mappingLocation) {
            this.mappingLocation = mappingLocation;
        }

        public String[] resolveMappingLocation() {
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            try {
                return Arrays.stream(resolver.getResources(this.mappingLocation)).map(it -> {
                    try {
                        return it.getURL().toString();
                    } catch (IOException e) {
                        e.printStackTrace();
                        return null;
                    }
                }).filter(Objects::nonNull).toArray(String[]::new);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
}
